#![allow(dead_code)]

use away_core::backgroup_service::job_params;
use away_db::entity::*;
use away_db::stock_repository::*;
use away_stock_data::stock::*;
use sea_orm::DbConn;

pub fn start(db: DbConn) {
    job_params("0 17 19 1/1 * ? ", db.clone(), update_stock_info);
}

async fn update_stock_info(db: DbConn) {
    tracing::info!("开始：更新股票信息任务");
    let mut pn = 2;
    let pz = 2000;
    loop {
        let Ok(res) = get_stock_page(pn, pz).await else{ break; };
        let data = res.data.diff;
        let len = data.len();

        let list = data
            .into_iter()
            .map(|o| stock_info::Model {
                id: 0,
                code: o.f12,
                name: o.f14,
                price: o.f2.as_f64().unwrap_or(0.0),
            })
            .collect();

        _ = stock_info_rep::update_stock_info(&db, list).await;

        if len < pz.try_into().unwrap() {
            break;
        }
        pn += 1;
    }
    tracing::info!("结束：更新股票信息任务");
}
